package com.five.community.controller;

import com.five.community.bean.Admin;
import com.five.community.bean.Role;
import com.five.community.service.AdminService;
import com.five.community.service.RoleService;
import com.five.community.util.FileUtils;

import com.five.community.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminService adminService;

    // 管理员列表画面
    @RequestMapping("/admin/listUi")
    public String listUi(ModelMap map) {

        try {
            List<Map<String, Object>> adminAndRoles = adminService.selectAllAdminAndRoles();
            map.addAttribute("adminAndRoles", adminAndRoles);
            // {password=111, salt=abcd, rname=服务模块管理员,
            // create_time=2022-08-16 14:28:10.0, header=aaa.png, id=1, state=0, username=aaa}
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "admin_list";
    }

    // 添加画面
    @RequestMapping("/admin/addUi")
    public String addUi(ModelMap map) {
        try {
            List<Role> roles = roleService.selectAll();
            map.addAttribute("roles", roles);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "admin_add";
    }

    // 添加
    @RequestMapping("/admin/add")
    public String add(Admin admin, MultipartFile headerFile) {


        String fileName = "";
        try {
            fileName = FileUtils.fileUpload(headerFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置数据
        admin.setHeader(fileName);
        admin.setCreateTime(new Date());

        // 加密
        // 设置随机盐
        String salt = SecurityUtils.getSalt();
        admin.setSalt(salt);
        // 设置盐加密后的密码
        String password = SecurityUtils.getMD5Password(admin.getPassword(), salt);
        // 重新设置加密后的密码
        admin.setPassword(password);

        try {
            adminService.insert(admin);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/admin/addUi";
    }


    // 登录画面
    @RequestMapping("/loginUi")
    public String loginUi() {
        return "login";
    }

    // 登录
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session) {
        try {
            Admin admin = adminService.login(username, password);
            if(admin != null) {
                // 登录成功
                session.setAttribute("admin", admin);
                return "redirect:/indexUi";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 登录失败
        return "redirect:/loginUi";
    }

    // 退出登录
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("admin");
        return "redirect:/loginUi";
    }
    //权限不足
    @RequestMapping("no")
    public String no(){
        return "no";
    }

}